// pages/bookdetail/bookdetail.js
const http = require('../../utils/http')
Page({
    /**
     * 页面的初始数据
     */
    data: {
        bookId: null,
        bookInfo: {},
        imageUrl: null,
        date: null
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (!options.bookId) {
            return;
        }
        this.setData({
            bookId: options.bookId,
        })
        http.get('/book/queryBookDetail', {
            bookId: this.data.bookId
        }, res => {
            this.setData({
                bookInfo: res.data.data,
                date:res.data.data.publishDate
            })
        }, error => {
            console.error(error)
        })
    },
    updateBookInfo(e) {
        const data = e.detail.value;
        data.id = this.data.bookId;
        if (this.data.imageUrl) {
            data.bookImgUrl = this.data.imageUrl;
        }
        http.post('/book/saveOrUpdate', data, res => {
            if (res.data.data === true) {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'error',
                    duration: 2000
                })
            }
        }, error => {
            console.error(error)
        })
    },
    deleteBookInfo() {
        let that = this;
        wx.showModal({
            title: '提示',
            content: '确认删除？',
            success(res) {
                if (res.confirm) {
                    let ids = [that.data.bookId];
                    http.post('/book/deleteBatch', JSON.stringify(ids), res => {
                        if (res.data.data === true) {
                            wx.showToast({
                                title: '删除成功',
                            })
                        } else {
                            wx.showToast({
                                title: '删除失败',
                            })
                        }
                    }, error => {
                        wx.showToast({
                            title: '删除失败',
                        })
                        console.log(error);
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    /**
     * 选择图片
     */
    chooseImage() {
        wx.chooseImage({
            count: 1, // 可选择的图片数量
            sizeType: ['compressed'], // 压缩图片
            sourceType: ['album', 'camera'], // 来源：相册或相机
            success: (res) => {
                // 将选择的图片上传到服务器
                this.uploadImage(res.tempFilePaths[0]);
            }
        })
    },
    /**
     * 图片上传
     * @param {*} imagePath 
     */
    uploadImage(imagePath) {
        let that = this;
        http.upload('/upload/uploadFile', imagePath, res => {
            let data = JSON.parse(res.data);
            // 上传成功后，将服务器返回的图片地址更新到image标签中
            that.setData({
                imageUrl: data.data.data
            });
        }, error => {
            console.error(error);
        })
    },
    bindDateChange(e) {
        this.setData({
            date: e.detail.value
        })
    }
})